package com.longcai.service.impl.homepage;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.entity.homepage.ClyjUserHotsearch;
import com.longcai.mapper.homepage.ClyjUserHotsearchMapper;
import com.longcai.service.homepage.ClyjUserHotsearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 * 用户热门搜索 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-04-02
 */
@Service
public class ClyjUserHotsearchServiceImpl extends ServiceImpl<ClyjUserHotsearchMapper, ClyjUserHotsearch> implements ClyjUserHotsearchService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjUserHotsearchMapper clyjUserHotsearchMapper;

    /**
     * 存储首页热门搜索
     *
     * @param name
     */
    @Override
    public void insertsearch(String name) {

        try {
            //查询是否存在
            QueryWrapper<ClyjUserHotsearch> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", name);
            queryWrapper.eq("flag", 0);
            ClyjUserHotsearch clyjUserHotsearch = this.clyjUserHotsearchMapper.selectOne(queryWrapper);
            if (clyjUserHotsearch != null) {
                clyjUserHotsearch.setNumber(clyjUserHotsearch.getNumber() + 1);
                this.clyjUserHotsearchMapper.updateById(clyjUserHotsearch);
            } else {
                ClyjUserHotsearch clyjUserHotsearch1 = new ClyjUserHotsearch();
                clyjUserHotsearch1.setId(UUID.randomUUID().toString().replace("-", ""));
                clyjUserHotsearch1.setName(name);
                clyjUserHotsearch1.setNumber(1);
                clyjUserHotsearch1.setFlag(0);
                this.clyjUserHotsearchMapper.insert(clyjUserHotsearch1);
            }
            logger.debug("修改成功");
        } catch (Exception e) {
            logger.error("修改失败");
            e.printStackTrace();
            e.getMessage();
        }

    }

    /**
     * 存储附近热门搜索
     *
     * @param keyvalue
     */
    @Override
    public void insertnearsearch(String keyvalue) {

        try {
            //查询是否存在
            QueryWrapper<ClyjUserHotsearch> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", keyvalue);
            queryWrapper.eq("flag", 1);
            ClyjUserHotsearch clyjUserHotsearch = this.clyjUserHotsearchMapper.selectOne(queryWrapper);
            if (clyjUserHotsearch != null) {
                clyjUserHotsearch.setNumber(clyjUserHotsearch.getNumber() + 1);
                this.clyjUserHotsearchMapper.updateById(clyjUserHotsearch);
            } else {
                ClyjUserHotsearch clyjUserHotsearch1 = new ClyjUserHotsearch();
                clyjUserHotsearch1.setId(UUID.randomUUID().toString().replace("-", ""));
                clyjUserHotsearch1.setName(keyvalue);
                clyjUserHotsearch1.setNumber(1);
                clyjUserHotsearch1.setFlag(1);
                this.clyjUserHotsearchMapper.insert(clyjUserHotsearch1);
            }
            logger.debug("修改成功");
        } catch (Exception e) {
            logger.error("修改失败");
            e.printStackTrace();
            e.getMessage();
        }
    }
}
